package com.hschinese.pronunciation.utils;

import android.content.Context;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

/**
 * Created by wo on 2015/11/4.
 */
public class WebRequestUtil {
    private Context context = null;
    private static WebRequestUtil mInstance = null;
    private RequestQueue requestQueue;
    private static final int TIME_OUT = 8000;

    private WebRequestUtil(Context cxt) {
        context = cxt;
        requestQueue = Volley.newRequestQueue(context);
    }

    public static WebRequestUtil getInstance(Context cxt) {
        synchronized (WebRequestUtil.class) {
            if (mInstance == null) {
                mInstance = new WebRequestUtil(cxt);
            }
            return mInstance;
        }
    }

    public void postRequestString(String url, final Map<String, String> params, Response.Listener<String> listener, Response.ErrorListener errorListener) {
        StringRequest postRequest = new StringRequest(Request.Method.POST, url, listener, errorListener) {
            @Override
            protected Map<String, String> getParams() {
                return params;
            }
        };
        postRequest.setRetryPolicy(new DefaultRetryPolicy(60 * 1000,0,0.1f));//设置超时  重试次数
        postRequest.setTag(url);
        log(url, params);
        requestQueue.add(postRequest);
        requestQueue.start();
    }

    private void log(final String url,final Map<String, String> params){
        StringBuilder sb = new StringBuilder();
        java.util.Iterator it = params.entrySet().iterator();
        while(it.hasNext()) {
            java.util.Map.Entry entry = (java.util.Map.Entry)it.next();
            if(entry.getKey() != null && entry.getValue() != null ){
                String key = entry.getKey().toString();
                String value = entry.getValue().toString();
                sb.append(key+"="+value+"&");
            }else{
                Logger.e("空值:"+entry.getKey());
            }
        }
        if(sb.length() > 0){
            sb = sb.delete(sb.length() - 1, sb.length());
        }
//		Common.println("请求参数:"+url+"?"+sb.toString()+"_sg:"+ Common.md5shot(d));
        Logger.e("传入参数:" + url + "?" + sb.toString());

    }

    public void uploadStudyRecord(final Map<String, String> params) {
        String url = "http://api.hschinese.com/talk/learn/records";
        StringRequest postRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    JSONObject json = new JSONObject(s);
                    Logger.e("xionghy - uploadRecord: " + json.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Logger.e("xionghy - onErrorResponse： " + volleyError.getMessage());
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                return params;
            }
        };
        postRequest.setRetryPolicy(new DefaultRetryPolicy(60 * 1000,0,0.1f));//设置超时  重试次数
    }

    /**
     * 取消Context下的所有请求
     * @param context
     */
    public void cancelAll(Context context){
        requestQueue.cancelAll(context);
    }

    /**
     * 取消本次请求
     * @param tag 请求的url
     */
    public void cancelByTag(Object tag){
        if(tag != null){
            requestQueue.cancelAll(tag);
        }
    }
}
